from glob import glob
import cv2
import os

train_image_dir = '/data/lzy/tile_round2_train_20210204/train_template_imgs/'
image_list = glob(os.path.join(train_image_dir, '*'))
save_dir = '/data/lzy/tile_round2_train_20210204/train_template_imgs_1to4/'
os.makedirs(save_dir, exist_ok=True)
for img_path in image_list:
    basename = os.path.basename(img_path)
    print("processing:{}".format(basename))
    image = cv2.imread(img_path)
    H, W, C = image.shape
    if(H >= 1500 or W >= 1500):
        half_h = H // 2
        half_w = W // 2
        image1 = image[:half_h, :half_w, :]
        image2 = image[:half_h, half_w:, :]
        image3 = image[half_h:, :half_w, :]
        image4 = image[half_h:, half_w:, :]
        name1 = os.path.join(save_dir, basename.split('.')[0] + '_1.jpg')
        name2 = os.path.join(save_dir, basename.split('.')[0] + '_2.jpg')
        name3 = os.path.join(save_dir, basename.split('.')[0] + '_3.jpg')
        name4 = os.path.join(save_dir, basename.split('.')[0] + '_4.jpg')
        cv2.imwrite(name1, image1)
        cv2.imwrite(name2, image2)
        cv2.imwrite(name3, image3)
        cv2.imwrite(name4, image4)
